import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import AboutView from '../views/AboutView.vue'
import EventsView from '../views/EventsView.vue'
import TimelineView from '../views/TimelineView.vue'
import TimeMailView from '../views/TimeMailView.vue'
import BlogView from '../views/BlogView.vue'
import BlogDetailView from '../views/BlogDetailView.vue'
import AiChatView from '../views/AiChatView.vue'
import WorkflowView from '../views/WorkflowView.vue'

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/blog',
    name: 'blog',
    component: BlogView
  },
  {
    path: '/blog/:id',
    name: 'blog-detail',
    component: BlogDetailView
  },
  {
    path: '/treehole',
    name: 'treehole',
    component: () => import('../views/TreeholeView.vue')
  },
  {
    path: '/game',
    name: 'game',
    component: () => import('../views/GameView.vue')
  },
  {
    path: '/about',
    name: 'about',
    component: AboutView
  },
  {
    path: '/contact',
    name: 'contact',
    component: () => import('../views/ContactView.vue')
  },
  {
    path: '/timeline',
    name: 'timeline',
    component: TimelineView
  },
  {
    path: '/events',
    name: 'events',
    component: EventsView
  },
  {
    path: '/time-mail',
    name: 'time-mail',
    component: TimeMailView
  },
  {
    path: '/ai-chat',
    name: 'ai-chat',
    component: AiChatView
  },
  {
    path: '/workflow',
    name: 'workflow',
    component: WorkflowView
  },
  {
    path: '/auth',
    name: 'auth',
    component: () => import('../views/LoginRegister.vue')
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

router.beforeEach((to, from, next) => {
  if (to.path === '/ai-chat') {
    const userId = localStorage.getItem('userId');
    if (!userId) {
      return next('/auth');
    }
  }
  next();
});

export default router 